<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @open="openDialog" @close="closeRoleDialog ">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="loading">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="submitEditRole">确定</el-button>
      <el-button size="small" @click="closeRoleDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from '@/api/setting'
import { reqAssignRoles } from '@/api/employees'
import { reqGetUserImgById } from '@/api/user'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      deefault: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      roleList: [],
      roleIds: []
    }
  },
  //   watch: {
  //     'userId': {
  //       async handler(val) {
  //         // console.log(val)
  //         this.userId = val
  //         this.loading = true
  //         const { data: res } = await reqGetUserImgById(this.userId)
  //         // console.log(res)
  //         this.roleIds = res.data.roleIds
  //         this.loading = false
  //       }
  //     }
  //   },

  methods: {
    openDialog() {
      this.loading = true
      Promise.all([this.getRolist(),
        this.getUserInfoById()]).then(() => {
        this.loading = false
      })
    },
    async getRolist() {
      const { data: { data }} = await reqGetRoleList(1, 100000)
      this.roleList = data.rows
    },
    async getUserInfoById() {
      const { data: res } = await reqGetUserImgById(this.userId)
      console.log(res)
      this.roleIds = res.data.roleIds
    },
    closeRoleDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    async submitEditRole() {
      console.log(this.userId)
      const res = await reqAssignRoles({ id: this.userId, roleIds: this.roleIds })
      console.log(res)
      this.closeRoleDialog()
    }
  }
}
</script>
